﻿
module egret3d
{
    export class UrlScene extends EventDispatcher
    {

        private canvas: Egret3DCanvas;
        private view1: View3D;
        private cameraCtl: LookAtController;

        private scene: MapLoader;
        private rolerScene: MapLoader;

        private cube: Mesh;
        private roler: Mesh;
        protected lights: LightGroup = new LightGroup();

        aniplayer: FreeNode.ForEgret3D.AniPlayer;
        anilist: string[];
        aniindex: number = 0;


        private fps: number = 60;
        private mesh: Mesh;
        private time: number;
        private meshBatcher: MeshBatcher;

        constructor()
        {
            super();
            this.canvas = new egret3d.Egret3DCanvas();
            
            this.canvas.x = 0;
            this.canvas.y = 0;
            this.canvas.width = window.innerWidth;
            this.canvas.height = window.innerHeight;

            this.view1 = new egret3d.View3D(0, 0, window.innerWidth, window.innerHeight);
            this.view1.backColor = 0xffC8EDCC;
            this.view1.camera3D.far = 1000000;
            
            var d: DirectLight = new DirectLight(new Vector3D(0, 0, -1));
            d.ambient = 0x32327D;
            this.lights.addLight(d);

            this.canvas.addView3D(this.view1);
            egret3d.Egret3DState.initState();
        }

        particleSystem: ParticleSystem;

        InitParticle()
        {
            this.time = 0;

            this.particleSystem = new ParticleSystem(this.view1);

            var material: MaterialBase = new MaterialBase();
            var img = new Image();
            img.onload = () =>
            {
                requestAnimationFrame(() =>
                {
                    material.diffuseTexture = new ImageTexture(img);
                }
                );
            };
            img.src = "1.jpg";
            this.particleSystem.setMat(material);
            this.particleSystem.addParticleByType("cube", (p, time) =>
            {
                if (!p.isinit)
                {
                    p.isinit = true;
                    p.rotDir = new Vector3D(0, 1, 0);
                    p.posDir.x = (Math.random() - 0.5) * 100;
                    p.posDir.y = Math.random() * 100;
                    p.posDir.z = (Math.random() - 0.5) * 100;
                    var scalnum = 0.5;
                    p.scaleVec.x = scalnum;
                    p.scaleVec.y = scalnum;
                    p.scaleVec.z = scalnum;

                    p.pos = new Vector3D(0, 0, 0);
                    p.isloop = true;//这个的初始化不应该在运动函数里
                }


                var rot = new egret3d.Quaternion();
                p.anglenum += time * 100;
                rot.fromAxisAngle(p.rotDir, p.anglenum);

                p.pos.x = p.pos.x + p.posDir.x;
                p.pos.y = p.pos.y + p.posDir.y;
                p.pos.z = p.pos.z + p.posDir.z;

                p.matrix.makeTransform(p.pos, p.scaleVec, rot);

                if (!p.alive)
                {
                    if (p.isloop)
                    {
                        p.matrix.identity();
                        p.alive = true;
                        p.curlifeTime = 0;
                        p.isinit = false;
                    }
                    else
                    {
                        p.matrix.makeTransform(new Vector3D(), new Vector3D(), rot);
                    }

                }

            }, 100);

            var material1: MaterialBase = new MaterialBase();
            var img1 = new Image();
            img1.onload = () =>
            {
                requestAnimationFrame(() =>
                {
                    material1.diffuseTexture = new ImageTexture(img1);
                }
                );
            };
            img1.src = "2.jpg";
            this.particleSystem.setMat(material1);
            this.particleSystem.addParticleByType("cube", (p, time) =>
            {
                if (!p.isinit)
                {
                    p.isinit = true;
                    p.rotDir = new Vector3D(0, 1, 0);
                    p.posDir.x = (Math.random() - 0.5) * 100;
                    p.posDir.y = Math.random() * 100;
                    p.posDir.z = (Math.random() - 0.5) * 100;
                    var scalnum = 0.5;
                    p.scaleVec.x = scalnum;
                    p.scaleVec.y = scalnum;
                    p.scaleVec.z = scalnum;

                    p.pos = new Vector3D(0, 0, 0);

                    p.isloop = false;//这个的初始化不应该在运动函数里
                }


                var rot = new egret3d.Quaternion();
                p.anglenum += time * 100;
                rot.fromAxisAngle(p.rotDir, p.anglenum);

                p.pos.x = p.pos.x + p.posDir.x;
                p.pos.y = p.pos.y + p.posDir.y;
                p.pos.z = p.pos.z + p.posDir.z;

                p.matrix.makeTransform(p.pos, p.scaleVec, rot);

                if (!p.alive)
                {
                    if (p.isloop)
                    {
                        p.matrix.identity();
                        p.alive = true;
                        p.curlifeTime = 0;
                        p.isinit = false;
                    }
                    else
                    {
                        p.matrix.makeTransform(new Vector3D(), new Vector3D(), rot);
                    }

                }
            }, 100);
            //this.particleSystem.addParticleByShape(this.getSelfPlaneShape(), (p, time) =>
            //{
            //    if (!p.isinit)
            //    {
            //        p.lifeTime = 10000;
            //        p.isinit = true;
            //    }
            //}, 1);
        }
        getSelfPlaneShape(): ParticleShape
        {
            var particleshape: ParticleShape = new ParticleShape();
            
            var _segmentsW: number = 10;
            var _segmentsH: number = 10;

            var _width: number = 2500.0;
            var _height: number = 2500.0;



            var x: number, y: number;
            var numIndices: number;
            var base: number;
            var tw: number = _segmentsW + 1;
            var numVertices: number = (_segmentsH + 1) * tw;
            var stride: number = particleshape.vertexAttLength;
            var skip: number = stride - 15;

            numIndices = _segmentsH * _segmentsW * 6;

            particleshape.vertexCount = numVertices;
            particleshape.indexCount = numIndices;


            numIndices = 0;
            var point: Vector3D = new Vector3D();
            var index: number = 0;
            for (var yi: number = 0; yi <= _segmentsH; ++yi)
            {
                for (var xi: number = 0; xi <= _segmentsW; ++xi)
                {
                    x = (xi / _segmentsW - .5) * _width;
                    y = (yi / _segmentsH - .5) * _height;
                    var z = 0;
                    var anglespan = 2 * Math.PI / _segmentsH;
                    if (xi == 0 || xi == _segmentsW || yi == 0 || yi == _segmentsH)
                    {

                    }
                    else
                    {
                        z = (Math.cos(anglespan * (xi - _segmentsW / 2)) + Math.cos(anglespan * (yi - _segmentsH / 2)) - 1) * 100;
                    }

                    particleshape.vertexArray[index++] = x;
                    particleshape.vertexArray[index++] = z;
                    particleshape.vertexArray[index++] = y;

                    particleshape.vertexArray[index++] = 0;
                    particleshape.vertexArray[index++] = 1;
                    particleshape.vertexArray[index++] = 0;


                    particleshape.vertexArray[index++] = 1;
                    particleshape.vertexArray[index++] = 0;
                    particleshape.vertexArray[index++] = 0;

                    particleshape.vertexArray[index++] = 1;
                    particleshape.vertexArray[index++] = 1;
                    particleshape.vertexArray[index++] = 1;
                    particleshape.vertexArray[index++] = 1;

                    particleshape.vertexArray[index++] = xi / _segmentsW;
                    particleshape.vertexArray[index++] = 1 - yi / _segmentsH;

                    index += skip;

                    if (xi != _segmentsW && yi != _segmentsH)
                    {
                        base = xi + yi * tw;
                        var mult: number = 1;

                        particleshape.indexArray[numIndices++] = base * mult;
                        particleshape.indexArray[numIndices++] = (base + tw + 1) * mult;
                        particleshape.indexArray[numIndices++] = (base + tw) * mult;

                        particleshape.indexArray[numIndices++] = base * mult;
                        particleshape.indexArray[numIndices++] = (base + 1) * mult;
                        particleshape.indexArray[numIndices++] = (base + tw + 1) * mult;

                    }
                }
            }
            return particleshape;
        }
        //update()
        //{
        //    this.time += 1000 / this.fps;
        //}
        public loadScene(sceneName: string)
        {
            this.InitParticle();

            //this.cube.position = new Vector3D(0, 8000, -25000);

            let cube: CubeGeometry = new CubeGeometry(1000,1000,1000);
            let mesh: Mesh = new Mesh(cube);
            mesh.visible = false;
            this.cameraCtl = new LookAtController(this.view1.camera3D, mesh);

            this.cameraCtl.distance = 8000;
            this.canvas.start();
            this.canvas.addEventListener(egret3d.Event3D.ENTER_FRAME, () => {
                if (this.cameraCtl) {
                    this.cameraCtl.update();
                }
            }, this);
            //this.canvas.addEventListener(Event3D.ENTER_FRAME, this.update, this);

            egret3d.Input.addEventListener(egret3d.Event3D.RESIZE, () => {
                this.canvas.width = window.innerWidth;
                this.canvas.height = window.innerHeight;
                this.view1.width = window.innerWidth;
                this.view1.height = window.innerHeight;
            }, this);


            ////新加的资源管理器，里面会用name 缓存资源
            var resmgr = FreeNode.ForEgret3D.ResourceMgr.instance();
            resmgr.init(this.lights);
        }
    }
}